from flask import Flask,render_template,request
import os,jsonpath,requests
from werkzeug.utils import secure_filename

app = Flask(__name__)
app.config['catalog']='catalog'
@app.route('/face')
def index1():
    return render_template('lianjie_123.html')

@app.route('/submit_image',methods = ['POST'])
def index():
        file = request.files.get('image')
        filename = secure_filename(file.filename)
        file_path = os.path.join(app.config['catalog'], filename)
        file.save(file_path)

        ###以下为人脸识别代码
        url = "http://localhost:8000/api/v1/recognition/recognize"

        api_key = "5c85256a-b627-4854-bc60-7a7918d0967c"

        headers = {
            'x-api-key': api_key,
        }

        files = {
            'file': (file_path, open(file_path, 'rb'), 'image/jpeg'),
        }

        response = requests.post(url, headers=headers, files=files)

        result1 = response.json()

        subject_list = jsonpath.jsonpath(result1, '$..subject')
        similarity_list = jsonpath.jsonpath(result1, '$..similarity')

        print(subject_list, similarity_list)

        def send_wechat(msg):
            token = '2cc51f9611034e5cb9614c5b948d3640'
            title = '人脸识别结果'
            content = msg
            template = 'html'
            url = f"https://www.pushplus.plus/send?token={token}&title={title}&content={content}&template={template}"
            print(url)
            r = requests.get(url=url)
            print(r.text)

        if __name__ == '__main__':
            if similarity_list[0] > 0.8:
                msg = subject_list[0]
                send_wechat(msg)
            else:
                msg = '陌生人'
                send_wechat(msg)
        ###

        return '照片已进行验证！'

if __name__ == '__main__':
    app.run()